home *** CD-ROM | disk | FTP | other *** search
/ The Original Shareware 1.1 / The Original Shareware (WeMake CDs)(Volume 1.1)(CDs, Inc)(1993).iso / 6 / dbf_tc.zip / LISTDB.C < prev   
Text File  |  1987-06-18  |  2KB  |  94 lines

  1. /*     file:        listdb.c
  2. **        purpose:    program to list the records in a dbaseiii file
  3. **        usage:    listdb filename
  4. **        notes:    compile with "tcc listdb d_open d_getrec d_getfld d_close"
  5. **        author:    Mark Sadler
  6. **        revised:    6/18/87
  7. */
  8.  
  9. #include <stdio.h>
  10. #include <alloc.h>
  11. #include <mem.h>
  12. #include "dbf.h"
  13.  
  14.  
  15. /* routine to format dbase date field -    buff in is in form    YYYYMMDD
  16.                                                         buff out is in form MM/DD/YY
  17. */
  18. void fdate(char *buff)
  19. {
  20.     int day;
  21.     int mon;
  22.     int yr;
  23.     if (buff[0]==' ')
  24.         {
  25.         buff[0]='\0';
  26.         strcat(buff,"  /  /  ");
  27.         }
  28.     else
  29.         {
  30.         sscanf(buff,"%4d%02d%02d",&yr,&mon,&day);
  31.         sprintf(buff,"%02d/%02d/%d",mon,day,yr-1900);
  32.         }
  33. }
  34.  
  35.  
  36. main(int argc,char **argv)
  37. {
  38.     int errornum;
  39.     long rec;
  40.     int fld;
  41.     struct DBF *d;
  42.     static char buff[255];
  43.  
  44.     if(argc != 2)
  45.     {
  46.         printf("Usage LISTDB filename\n");
  47.         exit(1);
  48.     }
  49.  
  50.     d = (struct DBF *)malloc(sizeof(struct DBF));        /* allocate space for DBF */
  51.  
  52.     strcpy(d->filename,argv[1]);
  53.  
  54.     if(!strchr(d->filename,'.'))                                         /* default to .dbf file     */
  55.         strcat(d->filename,".DBF");
  56.  
  57.  
  58.     if((errornum = d_open(d))!=0)                                                 /* open file                            */
  59.     {
  60.         printf("Error opening file: ");
  61.         switch (errornum)
  62.         {
  63.             case OUT_OF_MEM:
  64.                 printf("Not enough memory.\n");
  65.                 break;
  66.             case NO_FILE:
  67.                 printf("Can not open file %s.\n",d->filename);
  68.                 break;
  69.             case BAD_FORMAT:
  70.                 printf("File %s is not a dBASE III file.\n",d->filename);
  71.                 break;
  72.         }
  73.         free(d);
  74.         exit(1);
  75.     }
  76.  
  77.     for(rec=1;rec<=d->records;rec++)
  78.     {
  79.         printf("\n%-5u",rec);
  80.  
  81.         d_getrec(d,(long)rec);
  82.         for(fld=1;fld<=d->num_fields;fld++)
  83.         {
  84.             if(d_getfld(d,fld,buff) == 'D')
  85.                 fdate(buff);
  86.             printf(" %s",buff);
  87.         }
  88.     }
  89.  
  90.     d_close(d);
  91.     free(d);
  92. }
  93.  
  94.